// pages/list/list.js
import { getChannel, getList } from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId:'0',
    channelList: [{ name: '全部', id: '0' }],
    newsList:[],
    more: false,
    total: 0,
    page:1,
    pageSize:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  scrollToLower(e) {
    if (this.data.more) {
      return;
    }
    let listParams = {
      list_id: this.data.currentId,
      page: this.data.page+1,
      pageSize: this.data.pageSize
    }
    this.getMoreData(listParams)
  },
  getData(listParams){
    getList(listParams).then(data => {
      this.setData({
        newsList: data.data.list,
        total: data.data.total
      })
      if (this.data.total == this.data.newsList.length) {
        this.setData({
          more: true
        })
      }
    })
  },
  getMoreData(listParams) {
    getList(listParams).then(data => {
      this.setData({
        newsList: this.data.newsList.concat(data.data.list),
        total: data.data.total
      })
      if (this.data.total == this.data.newsList.length) {
        this.setData({
          more: true
        })
      }
    })
  },
  onLoad(options) {       
    console.log(options)
    let allList = options.info? JSON.parse(decodeURIComponent(options.info)):[]
    this.setData({
      currentId: options.id
    })
    
    if (allList.length>0){
      allList.forEach(item => {
        if (item.id == options.id) {
          this.setData({
            channelList: item['children']
          })
        }
      })
    }
      let listParams= {
        list_id: this.data.currentId,
        navbar: options.navbar ? options.navbar:'',
        navbar_id: options.navbar_id ? options.navbar_id:'',
        page: 1,
        pageSize: this.data.pageSize
      }
      this.getData(listParams)
    
  },
  onClick(e){
    this.setData({
      currentId: e.detail.name
    })
    let listParams = {
      list_id: this.data.currentId,
      page: 1,
      pageSize: this.data.pageSize
    }
    this.getData(listParams)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})